package optimizations.parser.SyntaxTree;

import java.io.IOException;
import java.util.List;

import execution.Tape;

import optimizations.lexer.Position;

public class LoopNode extends AbstractSyntaxNode {
	
	@Override
	public String toString() {
		return "LoopNode [child=" + child + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((child == null) ? 0 : child.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LoopNode other = (LoopNode) obj;
		if (child == null) {
			if (other.child != null)
				return false;
		} else if (!child.equals(other.child))
			return false;
		return true;
	}

	public List<AbstractSyntaxNode> getChild() {
		return child;
	}

	private final List<AbstractSyntaxNode> child;

	public LoopNode(Position position, List<AbstractSyntaxNode> child) {
		super(position, child);
		this.child = child;
	}

	@Override
	public void execute(Tape tape) throws IOException {
		while (tape.getDatum() != 0) {
			for (AbstractSyntaxNode node : this.getChildren()) {
				node.execute(tape);
			}
		}
	}

}
